package test 
{
	import fl.controls.Button;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileReference;
	import utils.Logger;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class MakeFurnXml extends Sprite
	{
		private var _btn:Button;
		private var xmlData:XML;
		private var _fileRef:FileReference;
		
		public function MakeFurnXml() 
		{
			addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
			
		}
		
		private function handleAddedToStage(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage);
			initPage();
		}
		
		private function initPage():void 
		{
			var _button:Sprite = new Sprite();
			drawButton(_button.graphics);
			_button.buttonMode = true;
			
			addChild(_button);
			
			_button.addEventListener(MouseEvent.CLICK, handleBtnClick);
			
			_fileRef = new FileReference();
			_fileRef.addEventListener(IOErrorEvent.IO_ERROR, hndlFileRefIOError);
			_fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, hndlFileRefSecurityErr);
			
			/*_btn.label = "Save";
			_btn.x = 320;
			_btn.y = 20;
			_btn.addEventListener(MouseEvent.CLICK, handleBtnClick);*/
			
			//prepareFurnUpB1();
			//prepareFurnUpB2OpenUp();
			//prepareFurnUpNiche();
			prepareFurnUpConvex();
		}
		
		/**
		 * up horiz, box 1
		 */
		private function prepareFurnUpB1():void {
			
			xmlData = <data></data>;
			
			var step:int = 5;
			var itemXml:XML;
			
			var furnWidthSM:Number = 15;
			var furnHeightSM:Number = 91.5;
			
			var furnWidthMM:Number = 0;
			var furnHeightMM:Number = furnHeightSM * 10;
			var furnDepthMM:Number = 300;
			
			for (; furnWidthSM <= 60; furnWidthSM += step ) {
				furnWidthMM = furnWidthSM * 10;
				
				itemXml = <item></item>;
				itemXml.@id = "up-d1#" + furnHeightSM.toString() + "-"  + furnWidthSM;
				itemXml.@title = "верхние";
				itemXml.@height = furnHeightMM.toString();
				itemXml.@width = furnWidthMM.toString();
				itemXml.@depth = furnDepthMM.toString();
				itemXml.@posY = "2000";
				
				var faceXml:XML = <face index="3"></face>
				var doorXml:XML = <door ul="0, 0.0" rd="1,1"></door>
				doorXml.@size = furnHeightSM.toString() + "-" + furnWidthSM.toString();
				log(furnHeightSM.toString() + "-" + furnWidthSM.toString());
				var handleXml:XML = <handle type="v" x="50" bottom="1" bottomPadding="50" />
				
				doorXml.appendChild(handleXml);
				faceXml.appendChild(doorXml);
				itemXml.appendChild(faceXml);
				
				xmlData.appendChild(itemXml);
			}
		}
		
		
		/**
		 * up horiz, box 2 open up
		 */
		private function prepareFurnUpB2OpenUp():void {
			
			xmlData = <data></data>;
			
			var step:int = 10;
			var itemXml:XML;
			
			var furnWidthSM:Number = 40;
			var furnHeightSM:Number = 72;
			
			var furnWidthMM:Number = 0;
			var furnHeightMM:Number = furnHeightSM * 10;
			var furnDepthMM:Number = 300;
			
			for (; furnWidthSM <= 100; furnWidthSM += step ) {
				furnWidthMM = furnWidthSM * 10;
				
				itemXml = <item></item>;
				itemXml.@id = "up-horiz-b2-o1#" + furnHeightSM.toString() + "-"  + furnWidthSM;
				itemXml.@title = "верхние";
				itemXml.@height = furnHeightMM.toString();
				itemXml.@width = furnWidthMM.toString();
				itemXml.@depth = furnDepthMM.toString();
				itemXml.@posY = "2000";
				
				var faceXml:XML = <face index="3"></face>
				
				// door1
				var door_1_Xml:XML = <door ul="0, 0.0" rd="1,0.5"></door>
					door_1_Xml.@size = "36-" + furnWidthSM.toString();
					door_1_Xml.@noextra="1";
				var handleXml:XML =  <handle type="h" centrx="1" bottom="1" bottomPadding="50"/>
				//door_1_Xml.appendChild(handleXml);
				
				// door2
				var door_2_Xml:XML = <door ul="0, 0.5" rd="1,1"></door>
					door_2_Xml.@size =  "36-" + furnWidthSM.toString();
				handleXml =  <handle type="h" centrx="1" bottom="1" bottomPadding="50"/>
				door_2_Xml.appendChild(handleXml);
				
				faceXml.appendChild(door_1_Xml);
				faceXml.appendChild(door_2_Xml);
				itemXml.appendChild(faceXml);
				
				xmlData.appendChild(itemXml);
			}
		}
		
			/**
		 * up horiz, box niche
		 */
		private function prepareFurnUpNiche():void {
			
			xmlData = <data></data>;
			
			
			var itemXml:XML;
			
			// setup
			var furnWidthSM:Number = 40;
			var furnHeightSM:Number = 91.5;
			var furnDepthSM:Number = 30;
			var step:int = 5;
			var endWidth:Number = 120;
			var prefixID:String = "u-nicheb-b2-o1";
			var title:String = "с нишей вверху, вверх одновременно";
			
			
			var furnWidthMM:Number = 0;
			var furnHeightMM:Number = furnHeightSM * 10;
			var furnDepthMM:Number = furnDepthSM * 10;
			
			for (; furnWidthSM <= endWidth; furnWidthSM += step ) {
				furnWidthMM = furnWidthSM * 10;
				
				itemXml = <item></item>;
				itemXml.@id = prefixID + "#" + furnHeightSM.toString() + "-"  + furnWidthSM;
				itemXml.@title = title;
				itemXml.@height = furnHeightMM.toString();
				itemXml.@width = furnWidthMM.toString();
				itemXml.@depth = furnDepthMM.toString();
				itemXml.@posY = "2000";
				
				var shelvesXml:XML = <shelves pos="0.787" force="1" />
				
				var faceXml:XML = <face index="3"></face>
				
				// door1
				var door_1_Xml:XML = <door ul="0, 0" rd="1, 0.393" noextra="1"></door>
					door_1_Xml.@size = "36-" + furnWidthSM.toString();
				var handleXml:XML =  <handle type="h" centrx="1" bottom="1" bottomPadding="50" />
				//door_1_Xml.appendChild(handleXml);
				
				// door2
				var door_2_Xml:XML = <door ul="0, 0.393" rd="1, 0.787"></door>
					door_2_Xml.@size = "36-" + furnWidthSM.toString();
				handleXml =  <handle type="h" centrx="1" bottom="1" bottomPadding="50" />
				door_2_Xml.appendChild(handleXml);
				
				faceXml.appendChild(door_1_Xml);
				faceXml.appendChild(door_2_Xml);
				
				itemXml.appendChild(shelvesXml);
				itemXml.appendChild(faceXml);
				
				xmlData.appendChild(itemXml);
			}
		}
		
		/**
		 * up convex, box niche
		 */
		private function prepareFurnUpConvex():void {
			
			xmlData = <data></data>;
			
			
			var itemXml:XML;
			
			// setup
			var furnWidthSM:Number = 40;
			var furnHeightSM:Number = 50;
			var furnDepthSM:Number = 37.5;
			var step:int = 5;
			var endWidth:Number = 100;
			var prefixID:String = "up-convex";
			var title:String = "Верхние однодверные выпуклые";
			
			
			var furnWidthMM:Number = 0;
			var furnHeightMM:Number = furnHeightSM * 10;
			var furnDepthMM:Number = furnDepthSM * 10;
			
			for (; furnWidthSM <= endWidth; furnWidthSM += step ) {
				furnWidthMM = furnWidthSM * 10;
				
				itemXml = <item></item>;
				itemXml.@id = prefixID + "#" + furnHeightSM.toString() + "-"  + furnWidthSM;
				itemXml.@title = title;
				itemXml.@height = furnHeightMM.toString();
				itemXml.@width = furnWidthMM.toString();
				itemXml.@depth = furnDepthMM.toString();
				itemXml.@posY = "2000";
				
				var shapePoints:XML = <shape points="[0, 0], [500, 0], [ 500, 300], [ 0, 300 , 250 , 450], [0, 0]" side="left" />
				
				var faceXml:XML = <face index="3"></face>
				
				// door1
				var door_1_Xml:XML = <door  ul="0,0" rd="1,1"></door>
					door_1_Xml.@size = "50-" + furnWidthSM.toString() + "convex";
				var handleXml:XML =  <handle type="h" centrx="1" bottom="1" bottomPadding="50" />
				door_1_Xml.appendChild(handleXml);
				
			
				faceXml.appendChild(door_1_Xml);
				
				itemXml.appendChild(shapePoints);
				itemXml.appendChild(faceXml);
				
				xmlData.appendChild(itemXml);
			}
		}
		
		
		private function hndlFileRefSecurityErr(e:SecurityErrorEvent):void 
		{
			log("Security error: " + e.text);
		}
		
		private function hndlFileRefIOError(e:IOErrorEvent):void 
		{
			log("IOError: " + e.text);
		}
		
		private function drawButton(gr:Graphics):void 
		{
			gr.beginFill(0xC0C0C0, 1);
			gr.lineStyle(1, 0x000000);
			gr.drawRect(0, 0, 150, 50);
			gr.endFill();
		}
		
		private function handleBtnClick(e:MouseEvent):void 
		{
			_fileRef.save(xmlData);
		}
		
		private function log(msg:String):void {
			Logger.log(this, msg);
		}
		
	}

}